package com.ray.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.google.appengine.api.search.query.QueryParser.restriction_return;
import com.ray.bean.CombinationKeyBean;
import com.ray.bean.CompanyMaterialTableBean;
import com.ray.bean.CustomerBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DepartmentBean;
import com.ray.bean.QueryOrderPageBean;
import com.ray.bean.ReportBean;
import com.ray.bean.SearchWhereBean;
import com.ray.bean.WorkshopProductionCycleBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.BasicDataMapper;
import com.ray.mapper.MaterialSelectFilterMapper;
import com.ray.mapper.ThirdPartyViewMapper;
import com.ray.pojo.MaterialSelectFilterPojo;

import net.sf.json.JSONArray;


@Service
public class ThirdPartyViewService {

	@Autowired
	ThirdPartyViewMapper thirdPartyViewMapper;
	@Autowired
	PartsProcedureService partsProcedureService;
	@Autowired
	OtherWarehouseService otherWarehouseService;
	@Autowired
	BasicDataService basicDataService;
	@Autowired
	OrderService orderService;
	@Autowired
	OrderProcedureTemplateService orderProcedureTemplateService;
	@Autowired
	ExpectDateService expectDateService;
	@Autowired
	PutOutServiceNew putOutServiceNew;
	@Autowired
	MaterialSelectFilterMapper materialSelectFilterMapper;
	@Autowired
	BasicDataMapper basicDataMapper;
    
	/**
	 * @name 客户登录
	 * @isUrl true
	 * @param coustomCompanyName(客户登录的公司)
	 * @param coustomUserName(登录的客户名称)
	 * @param coustomPassword(密码)
	 * @param map
	 * @return
	 */
	public CustomerBean login(Map map) {
		HttpServletRequest request=(HttpServletRequest) map.get("request");
		CustomerBean customerBean=thirdPartyViewMapper.getLoginCoustom(map);
		Assert.notNull(customerBean,"该账号不存在");
		request.getSession().setAttribute("thirdPartyViewLogin", customerBean);
		return customerBean;
	}
	
	/**
	 * @name 退出登录
	 * @isUrl true
	 * @param map
	 * @return
	 */
	public String loginOut(Map map) {
		HttpServletRequest request=(HttpServletRequest) map.get("request");
		request.getSession().setAttribute("thirdPartyViewLogin", null);
		return "login.html";
	}
	
	/**
	 * @name 获取订单工艺树
	 * @isUrl true
	 * @param orderId(订单编号)
	 * @param map
	 * @return
	 */
	public List<CombinationKeyBean> getAlreadySaveProcedure(Map map){
		return partsProcedureService.getAlreadySaveProcedure(map);
	}
	
	/**
	 * @name 获取订单查询要素
	 * @isUrl true
	 * @param map
	 * @return
	 */
	public List<SearchWhereBean> getAllOrderSearchWhere(Map map){
		return otherWarehouseService.getAllOrderSearchWhere(map);
	}
	
	
	/**
	  * @name 获取订单物料采购和入库时间
	  * @isUrl true
	  * @param orderId(订单编号)
	  * @author natelu
	  * @return
	  */
	public List<Map> getOrderMaterialParchaseAndInCountTime(Map map) {
		return partsProcedureService.getOrderMaterialParchaseAndInCountTime(map);
	}
	
	/**
	 * @name 获取公司的生产部门
	 * @isUrl true
	 * @param loginCompany
	 * @param map
	 * @return
	 */
	public List<DepartmentBean> getProductionDepartment(Map map) {
		return basicDataService.getProductionDepartment(map);
	}
	
	/**
	 * @name 获取部门下没合并的车间
	 * @isUrl true
	 * @param deptId
	 * @author nate
	 */
	public List<WorkshopProductionCycleBean> getDeptNoMegerWorkShop(Map map) {
		return basicDataService.getDeptNoMegerWorkShop(map);
	}
	
	/**
	 * @name 获取订单主表信息
	 * @isUrl true
	 * @param timeType(查询的时间类型,1制单时间 2交货日期)
	 * @param startDate(开始时间)
	 * @param date (结束时间)
	 * @param deptId(部门id)
	 * @param workShop (车间id)
	 * @param type (2 未完成 1 已完成)
	 * @param dutyStatus (拖期状态  1 拖期  2 即将拖期 )
	 * @param customerId (客户型号)
	 * @param customer (客户)
	 * @param companyOrderId (公司订单编号)
	 * @param factoryId (工厂型号)
	 * @param start(分页参数)
	 * @param length (分页参数)
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws CloneNotSupportedException 
	 */
	public Map getOrderMain(Map map) throws ParseException, CloneNotSupportedException {
		return orderService.getOrderMain(map);
	}
	
	
	/**
	 * @name 获取订单明细模式
	 * @isUrl true
	 * @param timeType(查询的时间类型,1制单时间 2交货日期)
	 * @param startDate(开始时间)
	 * @param date (结束时间)
	 * @param deptId(部门id)
	 * @param workShop (车间id)
	 * @param type (2 未完成 1 已完成)
	 * @param dutyStatus (拖期状态  1 拖期  2 即将拖期 )
	 * @param customerId (客户型号)
	 * @param customer (客户)
	 * @param companyOrderId (公司订单编号)
	 * @param factoryId (工厂型号)
	 * @param start(分页参数)
	 * @param length (分页参数)
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws CloneNotSupportedException 
	 */
	public Map queryOrderPageDateil(Map map) throws ParseException, CloneNotSupportedException {
		return orderService.queryOrderPageDateil(map);
	}
	
	/**
	 * @name 获取订单审核图片
	 * @isUrl true
	 * @param request
	 * @param orderId(订单编号)
	 * @return
	 */
	public String  getOrderApprovalImg(Map<String, Object> map) {
		return orderService.getOrderApprovalImg(map);
	}

	/**
	 * @name 获取订单明细采购入库情况
	 * @isUrl true
	 * @param orderId (订单编号)
	 * @param rowIndex (行号)
	 * @param map
	 * @return
	 */
	public List<QueryOrderPageBean> getOrderInfoDetail(Map map) {
		return orderService.getOrderInfoDetail(map);
	}
	
	/**
	 * @name 获取订单报工情况
	 * @isUrl true
	 * @param orderId(订单编号)
	 * @param map
	 * @return
	 */
	public List<WorkshopProductionCycleBean> getAleradyReportProcedure(Map map) {
		return orderProcedureTemplateService.getAleradyReportProcedure(map);
	}
	

	/**
	 * @name 获取工序生产时间
	 * @isUrl true
	 * @param map
	 * @return
	 * @throws ParseException
	 */
	public String getProcedureExpectDate(Map map) throws ParseException {
		return expectDateService.getProcedureExpectDate(map);
	}
	
	/**
	 * @name 获取订单工序报工数据
	 * @isUrl true
	 * @param orderId
	 * @param map
	 * @return
	 */
	public List<ReportBean> getProcedureReport(Map map) {
		return orderProcedureTemplateService.getProcedureReport(map);
	}
	
	
	/**
	 * @name 获取外协单数据
	 * @isUrl true
	 * @param start(分页参数)
	 * @param length(分页参数)
	 * @param type(类型 1   根据制单时间筛选   2 根据返回时间筛选  默认1)
	 * @param startTime(开始时间)
	 * @param endTime(结束时间)
	 * @param orderId(订单编号，选填)
	 * @param supplierId(外发供应商id，选填)
	 * @param technology(外发工艺，选填)
	 * @param maching(加工对象，选填)
	 * @param mantissa(尾数，选填)
	 * @param delay(拖期，选填)
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	public DatatablesViewPageBean getPutoutOrder(Map map) throws ParseException {
		map.put("loginUserId", "");
		return putOutServiceNew.getPutoutOrder(map);
	}
	
	
	/**
	 * @name 获取订单物料报工情况
	 * @isUrl true
	 * @param orderId(订单编号)
	 * @return
	 */
	public List<ReportBean> getOrderMaterialReport(Map map) {
		return orderService.getOrderMaterialReport(map);
	}
	
	/**
	 * 获取现货信息
	 */
	public List<MaterialSelectFilterPojo> getMaterialSelectData(Map<String, Object> map){
		return materialSelectFilterMapper.getMaterialSumMoneyData(map);
	}
	
	public DatatablesViewPageBean<CompanyMaterialTableBean> queryMaterialCompany(Map map) {
		int count = 0;
		DatatablesViewPageBean<CompanyMaterialTableBean> dvpb = new DatatablesViewPageBean<CompanyMaterialTableBean>();
		count = basicDataMapper.queryMaterialCompanyCount(map);
		dvpb.setReturnData(basicDataMapper.queryMaterialCompany(map));
		dvpb.setiTotalDisplayRecords(count);
		dvpb.setiTotalRecords(count);
		return dvpb;
	}
}
